//
//  SignatureViewController.swift
//  PDF
//
//  Created by Kellv on 2024/4/21.
//

import UIKit
import SignatureView
class SignatureViewController: CDBaseAllViewController {
    
    var signatureView: SignatureView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let appDelegate = UIApplication.shared.delegate as! CDAppDelegate
        appDelegate.blockRotation = true
     }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        let appDelegate = UIApplication.shared.delegate as! CDAppDelegate
        appDelegate.blockRotation = false
        
        //判断退出时是否是横屏
        if UIApplication.shared.statusBarOrientation.isLandscape {
            let resetOrientationTargert = NSNumber(integerLiteral: UIInterfaceOrientation.unknown.rawValue)
            UIDevice.current.setValue(resetOrientationTargert, forKey: "orientation")
            
            let orientationTarget = NSNumber(integerLiteral: UIInterfaceOrientation.portrait.rawValue)
            UIDevice.current.setValue(orientationTarget, forKey: "orientation")
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置当前控制器支持横屏
        let leftBtn = UIButton(type: .custom)
        leftBtn.frame = CGRect(x: 16, y: 16, width: 80, height: 32)
        leftBtn.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
        leftBtn.layer.cornerRadius = 16
        leftBtn.setTitleColor(UIColor(red: 0.498, green: 0.537, blue: 0.631, alpha: 1), for: .normal)
        leftBtn.setTitle("Cancel".localize(), for: .normal)
        leftBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 0.15)
        leftBtn.addTarget(self, action: #selector(onLeftAction), for: .touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        let rightBtn = UIButton(type: .custom)
        rightBtn.frame = CGRect(x: view.width - 16 - 80, y: 16, width: 80, height: 32)
        rightBtn.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
        rightBtn.layer.cornerRadius = 16
        rightBtn.addTarget(self, action: #selector(onDoneAction), for: .touchUpInside)
        
        let redo = UIButton(type: .custom)
        redo.setImage("redo".image, for: .normal)
        redo.addTarget(self, action: #selector(onRedoOrUndoAction), for: .touchUpInside)
        redo.tag = 1
        
        let undo = UIButton(type: .custom)
        undo.setImage("undo".image, for: .normal)
        undo.addTarget(self, action: #selector(onRedoOrUndoAction), for: .touchUpInside)
        self.navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: redo),UIBarButtonItem(customView: undo),UIBarButtonItem(customView: rightBtn)]
        redo.tag = 2


        
        let pen = UIButton(type: .custom)
        pen.frame = CGRect(x: signatureView.minX, y: signatureView.maxY + 15, width: 32, height: 32)
        pen.setImage("edit_pen_selected".image, for: .selected)
        pen.setImage("edit_pen".image, for: .normal)
        pen.addTarget(self, action: #selector(onPenOrEraserAction), for: .touchUpInside)
        self.view.addSubview(pen)
        pen.tag = 3
        
        let eraser = UIButton(type: .custom)
        eraser.frame = CGRect(x: pen.maxX + 16, y: pen.minY, width: 32, height: 32)
        eraser.setImage("edit_eraser".image, for: .normal)
        eraser.setImage("edit_eraser_selected".image, for: .selected)
        eraser.addTarget(self, action: #selector(onPenOrEraserAction), for: .touchUpInside)
        self.view.addSubview(eraser)
        eraser.tag = 3

    }

    
    @objc func onLeftAction() {
        self.dismiss(animated: true)
    }
    
    @objc func onDoneAction() {
        
    }
    
    @objc func onRedoOrUndoAction(sender: UIButton) {
        
    }
    
    @objc func onPenOrEraserAction(sender: UIButton) {
        
    }
}
